package com.fare.taluo.application.impl.crawl.crawlstatistics;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.crawl.crawlstatistics.FdcCrawlStatisticsApplication;
import com.fare.taluo.application.crawl.crawlstatistics.FdcCrawlStatisticsForFlightLineApplication;
import com.fare.taluo.core.domain.crawl.FdcCrawlStatisticsEntity;
import com.fare.taluo.core.domain.crawl.FdcCrawlStatisticsForFlightLineEntity;
import com.fare.taluo.core.domain.crawl.FdcCrawlStatisticsForGrabDateEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.TaskRecordEntityVO;
import com.fare.taluo.vo.crawl.statistics.FdcCrawlStatisticsForFlightLineVo;
import com.fare.taluo.vo.crawl.statistics.FdcCrawlStatisticsVO;
@Transactional
@Scope("singleton")
@Service("fdcCrawlStatisticsApplication")
public class FdcCrawlStatisticsApplicationImpl implements FdcCrawlStatisticsApplication{
	
//	@Resource(name ="fdcCrawlStatisticsForFlightLineApplication")
	@Inject
	private FdcCrawlStatisticsForFlightLineApplication fdcCrawlStatisticsForFlightLineApplication;

	/**
	 * 执行持久化存入数据中
	 * for history table 
	*/
	@Override
	public void saveDatas(List<TaskRecordEntityVO> listVo) {
		List<TaskRecordEntityVO> listVos=new ArrayList<TaskRecordEntityVO>();
		List<FdcCrawlStatisticsEntity> listEntity=new ArrayList<FdcCrawlStatisticsEntity>();
		List<FdcCrawlStatisticsEntity> updateListEntity=new ArrayList<FdcCrawlStatisticsEntity>();
		//过淲出不同一的航线Map<String ,list<TaskRecordEntityVO>>
		Map<String ,List<TaskRecordEntityVO>> mapListVo=filterByLine(listVo);
		FdcCrawlStatisticsEntity tp=new FdcCrawlStatisticsEntity();
		//处理每条航线数据
		for(Map.Entry<String , List<TaskRecordEntityVO>> maps:mapListVo.entrySet()){
			FdcCrawlStatisticsEntity entity=new FdcCrawlStatisticsEntity();//一个Map表示一条数据(只处理一天的数据)
			FdcCrawlStatisticsEntity updateEntity=new FdcCrawlStatisticsEntity();
			String citys[]=maps.getKey().split("-");
			List<TaskRecordEntityVO > listVoMap=maps.getValue();
			
			String grabChannelIdStr="",hbaseKeyStr="",channelNameStr="",flightDateInfoStr="",channelLinesStr="",channelSourceDataStr="";
			Long dataSources=0L;
			int flightDaysNums=0,failNums=0,successNums=0;
			Map<String,String> datesMap=new java.util.HashMap<String,String>();
			
			String fromCityCode=citys[0];
			String toCityCode=citys[1];
			//暂时30吧
			flightDaysNums=30;
			Map<String ,Integer> lineAndSource=new java.util.HashMap<String, Integer>();
			Map<String ,Long> sourceDataMap=new java.util.HashMap<String, Long>();
			Map<String,String> flightDateStrMap=new java.util.HashMap<String,String>();
			for(int i=0;i<listVoMap.size();i++){
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo=listVoMap.get(i);
				Long dataLengths=vo.getSourceDataSize();
				dataSources+=dataLengths;
				if(vo.getStatus().equals("3")){
					successNums++;
				}else{
					failNums++;
				}
				if(datesMap.get(vo.getFlightDate())==null){//说明不存在此航班日期
					datesMap.put(vo.getFlightDate(), vo.getFromCityOrg()+"-"+vo.getToCityOrg());
				}
				
				//组合flightDate
				//组合sourceDatas
//				flightDateInfoStr+=vo.getFlightDate()+",";
				flightDateStrMap.put(vo.getFlightDate(),vo.getFlightDate());
				String hbaseKey=vo.getSourceRowkey();
				if(lineAndSource.get(hbaseKey)!=null ){
					int lines=lineAndSource.get(hbaseKey)+1;
					lineAndSource.put(hbaseKey, lines);
					
					dataLengths+=sourceDataMap.get(hbaseKey);
					sourceDataMap.put(hbaseKey, dataLengths);
					
				}else{
					lineAndSource.put(hbaseKey, 1);
					sourceDataMap.put(hbaseKey, dataLengths);
					
					//组合渠道ＩＤ
					//组合渠道NAME
					//组合ＨＢＡＳＥＫＥＹ
					grabChannelIdStr+=vo.getGrabchannelID()+",";
					hbaseKeyStr+=vo.getSourceRowkey()+",";
					channelNameStr+=vo.getGrabChannel()+",";
				}
				
			}
			if(flightDateStrMap.size()>0){
				for(Map.Entry<String, String> mpsFlightDates:flightDateStrMap.entrySet()){
					flightDateInfoStr+=mpsFlightDates.getKey()+",";
				}
			}else{
				flightDateInfoStr="";
			}
			if(lineAndSource.size()>0){
				for(Map.Entry<String, Integer> lineMaps:lineAndSource.entrySet()){
					channelLinesStr+=lineMaps.getValue()+"@"+lineMaps.getKey()+",";
				}
			}
			if(sourceDataMap.size()>0){
				for(Map.Entry<String, Long>sourceDataMaps:sourceDataMap.entrySet()){
					channelSourceDataStr+=sourceDataMaps.getValue()+"@"+sourceDataMaps.getKey()+",";
				}
			}
			
			//入库查询
			String fromCityCodes=listVoMap.get(0).getFromCityOrg();
			String toCityCodes=listVoMap.get(0).getToCityOrg();
			java.sql.Date grabDates=null;
			if(listVoMap.get(0).getGrabDate() !=null){
				grabDates=new java.sql.Date((DateUtils.strToDate(listVoMap.get(0).getGrabDate(),"yyyy-MM-dd")).getTime());
			}
			List<FdcCrawlStatisticsEntity> listFindedEnity=tp.findEntity(fromCityCodes,toCityCodes , null,null, grabDates);
			if(listFindedEnity.size()==0){
				entity.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
	//			entity.setCreateTime(new SimpleDateFormat().parse(DateUtils.dateToStr(new Date(), "yyyy-MM-dd"),"yyyy-MM-dd HH:mm:ss"));
				entity.setGrabDate(grabDates);
				entity.setFromCity(fromCityCodes);
				entity.setFromCityName(listVoMap.get(0).getFromCityName());
				entity.setToCity(toCityCodes);
				entity.setToCityName(listVoMap.get(0).getToCityName());
				entity.setGrabChanelIdStr(grabChannelIdStr);
				entity.setGrabChannelName(channelNameStr);
				entity.setHbaseKey(hbaseKeyStr);
				entity.setChannelLinesInfo(channelLinesStr);
				entity.setChannelSourceDataInfo(channelSourceDataStr);
				entity.setFlightDateInfo(flightDateInfoStr);
				entity.setFailNum(failNums);
				entity.setSuccessNum(successNums);
				entity.setTotalNum(listVoMap.size());
				entity.setDataSources(dataSources);
				entity.setActualFlightDayNum(datesMap.size());
				entity.setFlightDaysNum(flightDaysNums);
				
				listEntity.add(entity);
			}else{//更新
				entity.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
				entity.setGrabDate(grabDates);
				entity.setFromCity(fromCityCodes);
				entity.setFromCityName(listVoMap.get(0).getFromCityName());
				entity.setToCity(toCityCodes);
				entity.setToCityName(listVoMap.get(0).getToCityName());
				
				String gcids=listFindedEnity.get(0).getGrabChanelIdStr();
				int allIdS=gcids.split("\\,").length;
				Map<String ,Integer> idMap=new HashMap<String ,Integer>();
				for(int m=0;m<allIdS;m++){
					idMap.put(gcids.split("\\,")[0],m+1);
				}
				int allIds2=channelSourceDataStr.split("\\,").length;
				for(int m2=0;m2<allIds2;m2++){
					idMap.put(channelSourceDataStr.split("\\,")[m2], m2);
				}
				String grabChanelIdStrs="";
				if(idMap.size()>0){
					for(Map.Entry<String, Integer> idMaps:idMap.entrySet()){
						grabChanelIdStrs+=idMaps.getKey()+",";
					}
				}
				updateEntity.setGrabChanelIdStr(grabChanelIdStrs);
				
				Map<String ,Integer> nameMap=new HashMap<String ,Integer>();
				String gcnames=listFindedEnity.get(0).getGrabChannelName();
				int allNameS=gcnames.split("\\,").length;
				for(int n=0;n<allNameS;n++){
					nameMap.put(gcnames.split("\\,")[n], n);
				}
				int allNameS2=channelNameStr.split("\\,").length;
				for(int n2=0;n2<allNameS2;n2++){
					nameMap.put(channelNameStr.split("\\,")[n2],n2);
				}
				String grabNameStrs="";
				if(nameMap.size()>0){
					for(Map.Entry<String, Integer> nameMaps:nameMap.entrySet()){
						grabNameStrs+=nameMaps.getKey()+",";
					}
				}
				updateEntity.setGrabChannelName(grabNameStrs);
				
				Map<String,Integer> hbaseKeyMap=new HashMap<String,Integer>();
				String gchbases=listFindedEnity.get(0).getHbaseKey();
				int allHbaseS=gchbases.split("\\,").length;
				for(int p=0;p<allHbaseS;p++){
					hbaseKeyMap.put(gchbases.split("\\,")[p],p);
				}
				int allHbaseS2=hbaseKeyStr.split("\\,").length;
				for(int p2=0;p2<allHbaseS2;p2++){
					hbaseKeyMap.put(hbaseKeyStr.split("\\,")[p2],p2);
				}
				String hbaseStrs="";
				if(hbaseKeyMap.size()>0){
					for(Map.Entry<String,Integer>hbaseMaps:hbaseKeyMap.entrySet() ){
						hbaseStrs+=hbaseMaps.getKey()+",";
					}
				}
				updateEntity.setHbaseKey(hbaseStrs);
				
				Map<String ,Integer>channelMap=new HashMap<String ,Integer>();
				String chlines=listFindedEnity.get(0).getChannelLinesInfo();
				int allChannelLineS=chlines.split("\\,").length;
				for(int q=0;q<allChannelLineS;q++){
					channelMap.put(chlines.split("\\,")[q],q);
				}
				int allChannelLineS2=channelLinesStr.split("\\,").length;
				for(int q2=0;q2<allChannelLineS2;q2++){
					channelMap.put(channelLinesStr.split("\\,")[q2], q2);
				}
				String channelLinesStrs="";
				if(channelMap.size()>0){
					for(Map.Entry<String, Integer> channelMaps:channelMap.entrySet()){
						channelLinesStrs+=channelMaps.getKey()+",";
					}
				}
				updateEntity.setChannelLinesInfo(channelLinesStrs);
				
				Map<String,Integer> channelSourceMap=new HashMap<String,Integer>();
				String chsources=listFindedEnity.get(0).getChannelSourceDataInfo();
				int allchSourceS=chsources.split("\\,").length;
				for(int t=0;t<allchSourceS;t++){
					channelSourceMap.put(chsources.split("\\,")[t], t);
				}
				int allchSourceS2=channelSourceDataStr.split("\\,").length; 
				for(int t2=0;t2<allchSourceS2;t2++){
					channelSourceMap.put(channelSourceDataStr.split("\\,")[t2], t2);
				}
				String channelSourceDataStrs="";
				if(channelSourceMap.size()>0){
					for(Map.Entry<String, Integer> channelSourceMaps:channelSourceMap.entrySet()){
						channelSourceDataStrs+=channelSourceMaps.getKey()+",";
					}
				}
				updateEntity.setChannelSourceDataInfo(channelSourceDataStrs);
				
				Map<String,Integer> flightDateInfoMap=new HashMap<String,Integer>();
				String flighDateInfos=listFindedEnity.get(0).getFlightDateInfo();
				int allflighDateInfoS=flighDateInfos.split("\\,").length;
				for(int r=0;r<allflighDateInfoS;r++){
					flightDateInfoMap.put(flighDateInfos.split("\\,")[r], r);
				}
				int allflighDateInfoS2=flightDateInfoStr.split("\\,").length;
				for(int r2=0;r2<allflighDateInfoS2;r2++){
					flightDateInfoMap.put(flightDateInfoStr.split("\\,")[r2], r2);
				}
				String flightDateInfoStrs="";
				if(flightDateInfoMap.size()>0){
					for(Map.Entry<String ,Integer> flightDateInfoMaps:flightDateInfoMap.entrySet()){
						flightDateInfoStrs+=flightDateInfoMaps.getKey()+",";
					}
				}
				updateEntity.setFlightDateInfo(flightDateInfoStrs);
				
				int failNumss=0;
				failNumss=failNums+listFindedEnity.get(0).getFailNum();
				updateEntity.setFailNum(failNumss);
				
				int successNumss=0;
				successNumss=successNums+listFindedEnity.get(0).getSuccessNum();
				updateEntity.setSuccessNum(successNumss);
				
				int TotalNums=0;
				TotalNums=listVoMap.size()+listFindedEnity.get(0).getTotalNum();
				updateEntity.setTotalNum(TotalNums);
				
				Long dataSourcess=0l;
				dataSourcess=dataSources+listFindedEnity.get(0).getDataSources();
				updateEntity.setDataSources(dataSourcess);
				
				int acturalSizes=0;
				acturalSizes=datesMap.size()+listFindedEnity.get(0).getActualFlightDayNum();
				updateEntity.setActualFlightDayNum(acturalSizes);
				
				int flightDaysNumss=0;
				flightDaysNumss=flightDaysNums+listFindedEnity.get(0).getFlightDaysNum();
				updateEntity.setFlightDaysNum(flightDaysNumss);
				
				updateListEntity.add(updateEntity);
			}
		}
		//入库前，判断今天是否已经执行过操作（是，更新之前入库的数据；重新入库）
		
		/*
		FdcCrawlStatisticsEntity tps=new FdcCrawlStatisticsEntity();
		if(listEntity.size()>0){
			for(FdcCrawlStatisticsEntity entitys:listEntity){
				Date grabDate=entitys.getGrabDate();
				String fromCity=entitys.getFromCity();
				String toCity=entitys.getToCity();
				//入库查询
				FdcCrawlStatisticsEntity tp=new FdcCrawlStatisticsEntity();
				List<FdcCrawlStatisticsEntity> listFindedEnity=tp.findEntity(fromCity, toCity, null,null, grabDate);
				if(listFindedEnity.size()>0){
					//删除数据by @fromCity @toCity @grabDate
					tp.deleteEntitys(fromCity, toCity, grabDate);
				}
			}
		}
		tps.saveDatas(listEntity);
		*/
		
		if(listEntity.size()>0){
			tp.saveDatas(listEntity);
		}
		if(updateListEntity.size()>0){
			tp.updateEntitys(updateListEntity);
		}
	}
	
	/**
	 * 根据航线过滤(for 历史数据)
	 */
	public static Map<String ,List<TaskRecordEntityVO>> filterByLine(List<TaskRecordEntityVO> listVo){
		Map<String ,List<TaskRecordEntityVO>> map=new LinkedHashMap<String ,List<TaskRecordEntityVO>>();	
		for(int i=0;i<listVo.size();i++){
			TaskRecordEntityVO old=listVo.get(i);
			String key=old.getFromCityOrg()+"-"+old.getToCityOrg()+"-"+old.getGrabDate();
			if(map.get(key)!=null){
				map.get(key).add(old);
			}else{
				List<TaskRecordEntityVO> newList=new ArrayList<TaskRecordEntityVO>();
				newList.add(old);
				map.put(key, newList);
			}
			
		}
		
		return map;
	}
	
	/**
	 * 执行持久化存入数据中
	 * for history grabDate table
	*/
	@Override
	public void saveDatasForGrabDate(List<TaskRecordEntityVO> listVo) {
		List<FdcCrawlStatisticsForGrabDateEntity> listEntity=new ArrayList<FdcCrawlStatisticsForGrabDateEntity>();
		Map<String ,List<TaskRecordEntityVO>> mapListVo=filterByLineByGrabDate(listVo);
		List<FdcCrawlStatisticsForGrabDateEntity> updateListEntity=new ArrayList<FdcCrawlStatisticsForGrabDateEntity>();
		
		//判断当前之前是否已经执行过持久化操作
		//Y:delete  N:update and insert
		FdcCrawlStatisticsForGrabDateEntity tp=new FdcCrawlStatisticsForGrabDateEntity();
		/*for(Map.Entry<String ,List<TaskRecordEntityVO>> getGrabDate:mapListVo.entrySet()){
			Date grabDate=new java.sql.Date(DateUtils.strToDate(getGrabDate.getKey(), "yyyy-MM-dd").getTime());
			List<FdcCrawlStatisticsForGrabDateEntity>  findByDate=tp.findByGrabDate(null, null,grabDate);
			if(findByDate.size()>0){
				tp.deleteEntityByGrabDate(grabDate);
			}
		}*/
		
		for(Map.Entry<String , List<TaskRecordEntityVO>> maps:mapListVo.entrySet()){
			FdcCrawlStatisticsForGrabDateEntity entity =new FdcCrawlStatisticsForGrabDateEntity();
			FdcCrawlStatisticsForGrabDateEntity updateEntity =new FdcCrawlStatisticsForGrabDateEntity();
			String grabDateStr=maps.getKey();
			List<TaskRecordEntityVO > listVoMap=maps.getValue();
			//当日抓取航线数量(排重)
			Map<String ,TaskRecordEntityVO> mapLinesMap=new java.util.HashMap<String ,TaskRecordEntityVO>();
			Long datTotalLines =0L,daySourceDatas=0L;
			String lineByDateTimeLines="";
			for(int k=0;k<listVoMap.size();k++){
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo=listVoMap.get(k);
				String key1=vo.getFromCityOrg()+"-"+vo.getToCityOrg();
				if(mapLinesMap.get(key1)==null){
					mapLinesMap.put(key1, vo);
				}
			}
			datTotalLines=(long) mapLinesMap.size();
			
			if(mapLinesMap.size()>0){
				for(Map.Entry<String ,TaskRecordEntityVO> linesMaps:mapLinesMap.entrySet()){
					lineByDateTimeLines+=linesMaps.getKey()+",";
				}
			}
			
			//当日抓取数据量
			for(int i=0;i<listVoMap.size();i++){
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo=listVoMap.get(i);
				daySourceDatas+=vo.getSourceDataSize();
			}
			//XX条航线@渠道
			Map<String ,List<TaskRecordEntityVO>> channelLines=new java.util.HashMap<String ,List<TaskRecordEntityVO>>();
			String linesForChannel="";
			for(int m=0;m<listVoMap.size();m++){
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo=listVoMap.get(m);
				String key2=vo.getSourceRowkey();
				String channelName=vo.getGrabChannel();
				if(channelLines.get(key2)!=null){
					channelLines.get(key2).add(vo);
					
				}else{
					List<TaskRecordEntityVO> listVoLine=new ArrayList<TaskRecordEntityVO>();
					listVoLine.add(vo);
					channelLines.put(key2, listVoLine);
				}
			}
			if(channelLines.size()>0){
				for(Map.Entry<String, List<TaskRecordEntityVO>> doMapLines: channelLines.entrySet()){
					//有些航线ā复了，因为航班日期不同；排重
					List<TaskRecordEntityVO> listSameCitys=doMapLines.getValue();
					Map<String ,TaskRecordEntityVO> sameMaps=new HashMap<String ,TaskRecordEntityVO>();
					if(listSameCitys.size()>0){
						for(int k=0;k<listSameCitys.size();k++){
							TaskRecordEntityVO vo=new TaskRecordEntityVO();
							vo=listSameCitys.get(k);
							String key3=vo.getFromCityOrg()+"-"+vo.getToCityOrg();
							if(sameMaps.get(key3)==null){
								sameMaps.put(key3, vo);
							}
						}
					}
					
					linesForChannel+=doMapLines.getKey()+"@"+sameMaps.size()+",";
				}
			}
			
			//XXM数据@渠道
			String sourcesForChannel="";
			Map<String ,TaskRecordEntityVO> channelSources=new java.util.HashMap<String ,TaskRecordEntityVO>();
			for(int n=0;n<listVoMap.size();n++){
				TaskRecordEntityVO vo=new TaskRecordEntityVO();
				vo=listVoMap.get(n);
				String key3=vo.getSourceRowkey();
				Long sourceSizes=vo.getSourceDataSize();
				if(channelSources.get(key3)!=null){
					TaskRecordEntityVO old= channelSources.get(key3);
					sourceSizes+=old.getSourceDataSize();
					channelSources.get(key3).setSourceDataSize(sourceSizes);
				}else{
					channelSources.put(key3, vo);
				}
			}
			if(channelSources.size()>0){
				for(Map.Entry<String, TaskRecordEntityVO> doMapSources: channelSources.entrySet()){
					sourcesForChannel+=doMapSources.getKey()+"@"+doMapSources.getValue().getSourceDataSize()+",";
				}
			}
			
			Date grabDateS=(grabDateStr !=null?new java.sql.Date((DateUtils.strToDate(grabDateStr,"yyyy-MM-dd")).getTime()):null);
			List<FdcCrawlStatisticsForGrabDateEntity>  findByDates=tp.findByGrabDate(null, null, grabDateS);
			//存数据前，入库查询判断，是否存在此抓取日期下的数据；存在，则更新入库
//			FdcCrawlStatisticsForGrabDateEntity tp=new FdcCrawlStatisticsForGrabDateEntity();
			if(findByDates.size()==0){
				entity.setGrabDate(grabDateS);
				entity.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
				entity.setDayTotalSourceData(daySourceDatas);
				entity.setDayTotalLineNumbers(datTotalLines);
				entity.setLineByChannel(linesForChannel);
				entity.setSourceByChannel(sourcesForChannel);
				entity.setLineCityByChannel(lineByDateTimeLines);
				listEntity.add(entity);
				
				if(listEntity.size()>0){//listEntity.size()==1
					tp.saveDatasForGrabDate(listEntity);
				}
				
				
			}else{
				//组合update数据
				updateEntity.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
				updateEntity.setGrabDate(grabDateS);
				Long dayTotalSourceDatas=findByDates.get(0).getDayTotalSourceData()+daySourceDatas;
				updateEntity.setDayTotalSourceData(dayTotalSourceDatas);
				
				Long totalLines=0l;
				Map<String ,Integer> dayLiensMap=new HashMap<String,Integer>();
				String LineCityByChannels=findByDates.get(0).getLineCityByChannel();
				int LineCityByChannelSizes=0;
				if(LineCityByChannels !=null){
					LineCityByChannelSizes=LineCityByChannels.split("\\,").length;
				}
				for(int m=0;m<LineCityByChannelSizes;m++){
					dayLiensMap.put(LineCityByChannels.split("\\,")[m], m);
				}
				int LineCityByChannelSizess=lineByDateTimeLines.split("\\,").length;
				for(int m2=0;m2<LineCityByChannelSizess;m2++){
					dayLiensMap.put(lineByDateTimeLines.split("\\,")[m2],m2);
				}
				String lineByDateTimeLiness="";
				if(dayLiensMap.size()>0){
					totalLines=(long)dayLiensMap.size();
					for(Map.Entry<String ,Integer>dayLiensMaps:dayLiensMap.entrySet()){
						lineByDateTimeLiness+=dayLiensMaps.getKey()+",";
					}
				}
				updateEntity.setDayTotalLineNumbers(totalLines);
				updateEntity.setLineCityByChannel(lineByDateTimeLiness);
				
				String linesForChannels="";
				int lineForChs=linesForChannel.split("\\,").length;
				Map<String ,Integer> lineByChannelMap=new HashMap<String,Integer>();
				for(int n=0;n<lineForChs;n++){
					String sigleLine=linesForChannel.split("\\,")[n];
					String channelHb=sigleLine.split("@")[0];
					int channelLineNum=Integer.parseInt(sigleLine.split("@")[1]);
					lineByChannelMap.put(channelHb, channelLineNum);
				}
				String linesForChannelss= findByDates.get(0).getLineByChannel();
				int lineForChss=linesForChannelss.split("\\,").length;
				for(int n2=0;n2<lineForChss;n2++){
					String key=linesForChannelss.split("\\,")[n2].split("@")[0];
					int vlues=Integer.parseInt(linesForChannelss.split("\\,")[n2].split("@")[1]);
					if(lineByChannelMap.get(key) !=null){
						lineByChannelMap.put(key,lineByChannelMap.get(key)+vlues);
					}else{
						lineByChannelMap.put(key, vlues);
					}
				}
				if(lineByChannelMap.size()>0){
					for(Map.Entry<String, Integer> lineByChannelMaps:lineByChannelMap.entrySet()){
						linesForChannels+=lineByChannelMaps.getKey()+"@"+lineByChannelMaps.getValue()+",";
					}
				}
				updateEntity.setLineByChannel(linesForChannels);
				
				String sourcesForChannels="";
				int sourceChannelS= sourcesForChannel.split("\\,").length;
				Map<String ,Long> sourceChannelMap=new HashMap<String,Long>();
				for(int t=0;t<sourceChannelS;t++){
					String sigleChannel=sourcesForChannel.split("\\,")[t];
					String chanelHbase=sigleChannel.split("@")[0];
					Long channelLineNumber=Long.parseLong(sigleChannel.split("@")[1]);
					sourceChannelMap.put(chanelHbase, channelLineNumber);
				}
				String sourcesForChannelss=findByDates.get(0).getSourceByChannel();
				int sourceChannelSS=sourcesForChannelss.split("\\,").length;
				for(int t2=0;t2<sourceChannelSS;t2++){
					String key=sourcesForChannelss.split("\\,")[t2].split("@")[0];
					Long values=Long.parseLong(sourcesForChannelss.split("\\,")[t2].split("@")[1]);
					if(sourceChannelMap.get(key) !=null){
						sourceChannelMap.put(key, sourceChannelMap.get(key)+values);
					}else{
						sourceChannelMap.put(key, values);
					}
				}
				if(sourceChannelMap.size()>0){
					for(Map.Entry<String, Long> sourceChannelMaps:sourceChannelMap.entrySet()){
						sourcesForChannels+=sourceChannelMaps.getKey()+"@"+sourceChannelMaps.getValue()+",";
					}
				}
				updateEntity.setSourceByChannel(sourcesForChannels);
				
				updateListEntity.add(updateEntity);
				
				if(updateListEntity.size()>0){//listEntity.size()==1
					tp.updateDatasForGrabDate(updateListEntity);
				}
			}
		}
		//FdcCrawlStatisticsForGrabDateEntity tp=new FdcCrawlStatisticsForGrabDateEntity();
		//存数据前，入库查询判断，是否存在此抓取日期下的数据；存在，则删除，重新入库
		/*if(listEntity.size()>0){
			for(FdcCrawlStatisticsForGrabDateEntity entityByGrab:listEntity){
				List<FdcCrawlStatisticsForGrabDateEntity>  findByDate=tp.findByGrabDate(null, null, entityByGrab.getGrabDate());
				if(findByDate.size()>0){
					tp.deleteEntityByGrabDate(entityByGrab.getGrabDate());
				}
			}
		}
		tp.saveDatasForGrabDate(listEntity);*/
	}
	
	/**
	 * 根据航线过滤(for 按抓取日期分类)
	 */
	public static Map<String ,List<TaskRecordEntityVO>> filterByLineByGrabDate(List<TaskRecordEntityVO> listVo){
		Map<String ,List<TaskRecordEntityVO>> map=new LinkedHashMap<String ,List<TaskRecordEntityVO>>();
		for(int i=0;i<listVo.size();i++){
			TaskRecordEntityVO old=listVo.get(i);
			String key=old.getGrabDate();
			if(map.get(key)!=null){
				map.get(key).add(old);
			}else{
				List<TaskRecordEntityVO> newList=new ArrayList<TaskRecordEntityVO>();
				newList.add(old);
				map.put(key, newList);
			}
		}
		return map;
	}
	
	/**
	 * 执行持久化存入数据中
	 * for grab line table 
	*/
	@SuppressWarnings("unchecked")
	@Override
	public void saveDatasForFlightLine(List<TaskRecordEntityVO> listVo) {
		List<FdcCrawlStatisticsForFlightLineEntity> insertListEntity=new ArrayList<FdcCrawlStatisticsForFlightLineEntity>();
		List<FdcCrawlStatisticsForFlightLineEntity> updateListEntity=new ArrayList<FdcCrawlStatisticsForFlightLineEntity>();
		Map<String ,List<TaskRecordEntityVO>> mapListVo=filterByLineByLine(listVo);
		for(Map.Entry<String , List<TaskRecordEntityVO>> maps:mapListVo.entrySet()){
			String cityFlegs[]=maps.getKey().split("-");
			String fromCityCode=cityFlegs[0];
			String toCityCode=cityFlegs[1];
			String fromCityName=null,toCityName=null,toCityNameQ=null,fromCityNameQ=null;
			Long totalSourceSizes=0l;
			Date startGrabDate=null,endGrabDate=null;
			/*
			 * 每次执行更新时，进入数据库先查询此航线是否存在
			 * 存在由执行更新操作，不存在则插入
			 */			
			List<TaskRecordEntityVO > listVoMap= maps.getValue();
			Map<String ,String> flightDatesMap=new HashMap<String ,String>();
			List<String> flighDatesList=new ArrayList<String>();
			//过滤抓取日期
			for(TaskRecordEntityVO vo:listVoMap){
				totalSourceSizes+=vo.getSourceDataSize();
				flightDatesMap.put(vo.getGrabDate(),vo.getLeg());
				fromCityNameQ=vo.getFromCityName();
				toCityNameQ=vo.getToCityName();
			}
			//把MAP日期放入到LIST
			if(flightDatesMap.size()>0){
				for(Map.Entry<String, String> mapList:flightDatesMap.entrySet()){
					flighDatesList.add(mapList.getKey());
				}
			}
			//排序抓取日期List
			sortClass sort = new sortClass();
			Collections.sort(flighDatesList,sort);//降序排列
			
			Date endGrabDateQ=(flighDatesList.get(0) !=null?new java.sql.Date((DateUtils.strToDate(flighDatesList.get(0),"yyyy-MM-dd")).getTime()):null);
			Date startGrabDateQ=(flighDatesList.get(flighDatesList.size()-1) !=null?new java.sql.Date((DateUtils.strToDate(flighDatesList.get(flighDatesList.size()-1),"yyyy-MM-dd")).getTime()):null);
//			List <FdcCrawlStatisticsForFlightLineVo> listFLvo=InstanceFactory.getInstance(FdcCrawlStatisticsForFlightLineApplication.class).findForFlightLineVo(fromCityCode, toCityCode);
			List <FdcCrawlStatisticsForFlightLineVo> listFLvo=fdcCrawlStatisticsForFlightLineApplication.findForFlightLineVo(fromCityCode, toCityCode);
			FdcCrawlStatisticsForFlightLineVo vo=new FdcCrawlStatisticsForFlightLineVo();
			if(listFLvo.size()!=0){
				vo=listFLvo.get(0);//理论上是只有一条数据的，如果出现两条以上，说明此表一问题
				startGrabDate=vo.getGrabEndDate();
				fromCityName=vo.getFromCityName();
				toCityName=vo.getToCityName();
			}
			
			//insert operation
			if(listFLvo.size()<0 || listFLvo.size()==0){//插入
				FdcCrawlStatisticsForFlightLineEntity insertEntity=new FdcCrawlStatisticsForFlightLineEntity();
				insertEntity.setToCity(toCityCode);
				insertEntity.setFromCity(fromCityCode);
				
				insertEntity.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
				insertEntity.setGrabStartDate(startGrabDateQ);
				insertEntity.setGrabEndDate(endGrabDateQ);
				insertEntity.setFromCityName(fromCityNameQ);
				insertEntity.setToCityName(toCityNameQ);
				insertEntity.setGrabTotalSource(totalSourceSizes);
				insertListEntity.add(insertEntity);
			}
			
			//update operation
			if(listFLvo.size()>0){//修改
				FdcCrawlStatisticsForFlightLineEntity updateEntity=new FdcCrawlStatisticsForFlightLineEntity();
				updateEntity.setFromCity(fromCityCode);
				updateEntity.setToCity(toCityCode);
				for(FdcCrawlStatisticsForFlightLineVo vo2:listFLvo){
					totalSourceSizes+=vo2.getGrabTotalSource();
				}
				//比较endGrabDate
				if(DateUtils.complareDate(endGrabDate, startGrabDateQ)){
					updateEntity.setGrabEndDate(endGrabDate);
				}else{
					updateEntity.setGrabEndDate(startGrabDateQ);
				}
				updateEntity.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
				updateEntity.setGrabStartDate(startGrabDate);
				updateEntity.setFromCityName(fromCityName);
				updateEntity.setToCityName(toCityName);
				updateEntity.setGrabTotalSource(totalSourceSizes);
				updateListEntity.add(updateEntity);
			}
		}
		
		FdcCrawlStatisticsForFlightLineEntity tp=new FdcCrawlStatisticsForFlightLineEntity();
		if(insertListEntity.size()>0){
			tp.saveDatasForFlightLine(insertListEntity);
		}
		if(updateListEntity.size()>0){
			tp.updateDatasForFlightLine(updateListEntity);
		}
		
	}
	
	/**
	 * 根据航线过滤(for 按抓取航线分类)
	 */
	public static Map<String ,List<TaskRecordEntityVO>> filterByLineByLine(List<TaskRecordEntityVO> listVo){
		Map<String ,List<TaskRecordEntityVO>> map=new LinkedHashMap<String ,List<TaskRecordEntityVO>>();
		for(int i=0;i<listVo.size();i++){
			TaskRecordEntityVO old=listVo.get(i);
			String key=old.getFromCityOrg()+"-"+old.getToCityOrg();
			if(map.get(key)!=null){
				map.get(key).add(old);
			}else{
				List<TaskRecordEntityVO> newList=new ArrayList<TaskRecordEntityVO>();
				newList.add(old);
				map.put(key, newList);
			}
		}
		return map;
	}

	@SuppressWarnings("rawtypes")
	public class sortClass implements Comparator {
		public int compare(Object arg0, Object arg1) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String user0 = (String) arg0;
			String user1 = (String) arg1;
			java.util.Date d0=null;
			java.util.Date d1=null;
			if(user0 !=null){
				try {
					d0=sdf.parse(user0);
					d1=sdf.parse(user1);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			if (d1 == null && d0 == null) {
				return 0;
			}
			if (d1 == null) {
				return -1;
			}
			if (d0 == null) {
				return 1;
			}
			 return d1.compareTo(d0);
		}
	}

	@Override
	public List<FdcCrawlStatisticsVO> findByGrabDate(String fromCity,String toCity,String dateStr, String dateStr2) {
		List<FdcCrawlStatisticsVO> listVo=new ArrayList<FdcCrawlStatisticsVO>();
		FdcCrawlStatisticsEntity tp=new FdcCrawlStatisticsEntity();
		Date date1=null,date2=null;
		if(dateStr !=null && !dateStr.equals("") ){
			date1=new java.sql.Date((DateUtils.strToDate(dateStr,"yyyy-MM-dd")).getTime());
		}
		if(dateStr2!=null && !dateStr2.equals("")){
			date2=new java.sql.Date((DateUtils.strToDate(dateStr2,"yyyy-MM-dd")).getTime());
		}
		List<FdcCrawlStatisticsEntity>listEntity=tp.findEntity(fromCity,toCity,date1, date2,null);
		if(listEntity.size()>0){
			for(FdcCrawlStatisticsEntity entity:listEntity){
				FdcCrawlStatisticsVO vo=new FdcCrawlStatisticsVO();
				vo.setCreateTime(entity.getCreateTime());
				vo.setActualFlightDaysNum(entity.getActualFlightDayNum());
				vo.setDataSources(entity.getDataSources());
				vo.setFailNum(entity.getFailNum());
				vo.setFlighDaysNum(entity.getFlightDaysNum());
				vo.setFromCity(entity.getFromCity());
				vo.setFromCityName(entity.getFromCityName());
				vo.setGrabChannelHbaseKey(entity.getHbaseKey());
				vo.setGrabChannelIdStr(entity.getGrabChanelIdStr());
				vo.setGrabChannelName(entity.getGrabChannelName());
				vo.setGrabDate(entity.getGrabDate());
				vo.setGrabDateStr(entity.getGrabDate() !=null?DateUtils.dateToStr(entity.getGrabDate(),"yyyy-MM-dd"):null);
				vo.setId(Long.parseLong(entity.getId()));
				vo.setSuccessNum(entity.getSuccessNum());
				vo.setToCity(entity.getToCity());
				vo.setToCityName(entity.getToCityName());
				vo.setTotalNum(entity.getTotalNum());
				
				listVo.add(vo);
			}
		}
		
		return listVo;
	}
	
}
